//
//  AXFTripleBtnsCell.m
//  BEEQUICK
//
//  Created by admin on 20/11/2016.
//  Copyright © 2016 SevenGroup. All rights reserved.
//

#import "AXFTripleBtnsCell.h"

@interface AXFTripleBtnsCell ()

@property (weak, nonatomic) UIButton *lastSelectedBtn;
@property (strong, nonatomic) NSArray<UIButton *> *btnArr;
@property (weak, nonatomic) UIView *redView;

@end

@implementation AXFTripleBtnsCell

- (void)awakeFromNib {
	[super awakeFromNib];
	// Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
	if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
		[self setupUI];
	}
	return self;
}

- (void)setupUI {
	NSArray<NSString *> *btnNames = @[@"未使用", @"已使用", @"已过期"];
	NSMutableArray<UIButton *> *btnArrM = [NSMutableArray arrayWithCapacity:3];
	[btnNames enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
		UIButton *btn = [self setupBtnWithText:obj];
		[btnArrM addObject:btn];
	}];
	NSArray<UIButton *> *btnArr = btnArrM.copy;
	_btnArr = btnArr;
	
	[btnArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
	[btnArr mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerY.equalTo(self.contentView);
	}];
	
	btnArr[0].selected = YES;
	_lastSelectedBtn = btnArr[0];
	
	UIView *redView = [[UIView alloc] init];
	redView.backgroundColor = [UIColor cz_colorWithHex:0xff4400];
	redView.userInteractionEnabled = NO;
	[self.contentView addSubview:redView];
	
	_redView = redView;
	
	[_redView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.width.equalTo(@94);
		make.height.equalTo(@3);
		make.bottom.equalTo(self.contentView);
		make.centerX.equalTo(_btnArr[0]).offset(0);
	}];
}

- (UIButton *)setupBtnWithText:(NSString *)text {
	UIButton *btn = [UIButton cz_textButton:text fontSize:14 normalColor:[UIColor blackColor] selectedColor:[UIColor cz_colorWithHex:0xff6831]];
	[self.contentView addSubview:btn];
	[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
	return btn;
}

- (void)btnAction:(UIButton *)btn {
	NSLog(@"%@", btn.titleLabel.text);
	self.lastSelectedBtn.selected = NO;
	btn.selected = YES;
	self.lastSelectedBtn = btn;
	
	NSInteger index = [_btnArr indexOfObject:btn];
	
	NSLog(@"%zd",index);
	[_redView mas_updateConstraints:^(MASConstraintMaker *make) {
		make.centerX.equalTo(_btnArr[0]).offset(index * (1.0 / 3) * kSCREENWIDTH);
	}];
	[UIView animateWithDuration:0.5 animations:^{
		[self layoutIfNeeded];
	}];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
	[super setSelected:selected animated:animated];
	
	// Configure the view for the selected state
}

@end
